
package com.joysuch.wwyt.bp.service.impl;

import com.joysuch.wwyt.bp.bean.BpStoreAreaAddBean;
import com.joysuch.wwyt.bp.bean.BpStoreAreaBean;
import com.joysuch.wwyt.bp.entity.BpDeviceFacilityCollection;
import com.joysuch.wwyt.bp.entity.BpMonitoringPointRel;
import com.joysuch.wwyt.bp.entity.BpStoreArea;
import com.joysuch.wwyt.bp.entity.BpStoreHouse;
import com.joysuch.wwyt.bp.entity.dsl.QBpStoreHouse;
import com.joysuch.wwyt.bp.enums.DeviceFacilityTypes;
import com.joysuch.wwyt.bp.repository.BpMonitoringPointRelDao;
import com.joysuch.wwyt.bp.repository.BpStoreAreaDao;
import com.joysuch.wwyt.bp.repository.BpStoreHouseDao;
import com.joysuch.wwyt.bp.service.BpDeviceFacilityCollectionService;
import com.joysuch.wwyt.bp.service.BpStoreAreaService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.mh.bean.MhMonitoringPointBean;
import com.joysuch.wwyt.mh.entity.MhMajorHazard;
import com.joysuch.wwyt.mh.repository.MhMajorHazardDao;
import com.joysuch.wwyt.mh.service.MhMonitoringPointService;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

import static java.util.Collections.EMPTY_LIST;

@Service
@Transactional
public class BpStoreAreaServiceImpl implements BpStoreAreaService {

    @Autowired
    private BpStoreAreaDao dao;
    @Autowired
    private BpStoreHouseDao bpStoreHouseDao;
    @Autowired
    private BpMonitoringPointRelDao relDao;
    @Autowired
    private MhMonitoringPointService mhMonitoringPointService;
    @Autowired
    private BpDeviceFacilityCollectionService bpDeviceFacilityCollectionService;
    @Autowired
    private MhMajorHazardDao mhMajorHazardDao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;

    @Override
    public ResultBean list(Pageable of) {
        Page<BpStoreArea> all = dao.findAll(of);
        ArrayList<BpStoreAreaBean> list = new ArrayList<>();
        for (BpStoreArea bpStoreArea : all) {
            BpStoreAreaBean b = new BpStoreAreaBean();
            b.setId(bpStoreArea.getId());
            b.setTenentId(bpStoreArea.getTenentId());
            b.setCode(bpStoreArea.getCode());
            b.setName(bpStoreArea.getName());
            b.setCreateBy(bpStoreArea.getCreateBy());
            b.setDeleteFlag(bpStoreArea.getDeleteFlag());
            b.setCreateTime(bpStoreArea.getCreateTime());
            b.setEnvFunctionArea(bpStoreArea.getEnvFunctionArea());
            b.setLocation(bpStoreArea.getLocation());
            if (bpStoreArea.getMajorHazardId() != null) {
                b.setMajorHazardUnit(mhMajorHazardDao.getOne(bpStoreArea.getMajorHazardId()).getName());
            }
            b.setMajorHazardUnitCode(bpStoreArea.getMajorHazardUnitCode());
            b.setNearbyStoreHouseMinDistance(bpStoreArea.getNearbyStoreHouseMinDistance());
            b.setOrgCode(bpStoreArea.getOrgCode());
            b.setStoreaAreaSize(bpStoreArea.getStoreaAreaSize());
            b.setSurrEquipmentDistance(bpStoreArea.getSurrEquipmentDistance());
            b.setUpdateTime(bpStoreArea.getUpdateTime());
            b.setUpdateBy(bpStoreArea.getUpdateBy());
            b.setSurrSafetyDistance(bpStoreArea.getSurrSafetyDistance());
            Page<BpStoreHouse> all1 = bpStoreHouseDao.findAll(of);
            of = PageDataUtils.addOrderByDesc(of, "id");
            ArrayList<BpStoreHouse> objects = new ArrayList<>(0);
//            for (BpStoreHouse bpStoreHouse : all1) {
//
//                if (bpStoreArea.getCode().equals(bpStoreHouse.getStoreAreaCode())) {
//                    if(bpStoreHouse.getMajorMonitoringChemFlag().equals("Y")){
//                        bpStoreHouse.setMajorMonitoringChemFlag("是");
//                    }
//                    if(bpStoreHouse.getMajorMonitoringChemFlag().equals("N")){
//                        bpStoreHouse.setMajorMonitoringChemFlag("否");
//                    }
//                    if(bpStoreHouse.getMajorMonitoringChemFlag().equals(null)){
//                        bpStoreHouse.setMajorMonitoringChemFlag(null);
//                    }
//
//                    objects.add(bpStoreHouse);
//                    b.setBpStoreHouse(objects);
//                }
//            }
            if (objects.isEmpty()) {
                b.setBpStoreHouse(EMPTY_LIST);
            }
            list.add(b);
        }
        return ResultBean.pageData(list, all.getTotalElements());
    }

    @Override
    public void add(BpStoreAreaAddBean b) {
        BpStoreArea a = new BpStoreArea();

        if (StringUtils.isEmpty(b.getCode())) {
            a.setCode(bizCodeGeneratorFacade.kuQu());
        } else {
            a.setCode(b.getCode());
        }
        a.setEnvFunctionArea(b.getEnvFunctionArea());
        a.setLocation(b.getLocation());
        a.setMajorHazardId(b.getMajorHazardId());
        a.setMajorHazardUnitCode(b.getMajorHazardUnitCode());
        a.setName(b.getName());
        a.setNearbyStoreHouseMinDistance(b.getNearbyStoreHouseMinDistance());
        a.setStoreaAreaSize(b.getStoreaAreaSize());
        a.setSurrEquipmentDistance(b.getSurrEquipmentDistance());
        a.setSurrSafetyDistance(b.getSurrSafetyDistance());
        BpStoreArea save = dao.save(a);
        List<Long> pointId = b.getPointId();
        for (Long aLong : pointId) {
            BpMonitoringPointRel bpMonitoringPointRel = new BpMonitoringPointRel();
            bpMonitoringPointRel.setRefType("store_area");
            bpMonitoringPointRel.setRefId(save.getId());
            bpMonitoringPointRel.setMonitoringPointId(aLong);
            relDao.save(bpMonitoringPointRel);
        }
        addOrUpdateDeviceFacilityCollection(save, pointId);
    }

    private void addOrUpdateDeviceFacilityCollection(BpStoreArea save, List<Long> pointId) {
        // 保存到集中统一的表中
        BpDeviceFacilityCollection device = bpDeviceFacilityCollectionService.addOrUpdateOtherDevice(
                DeviceFacilityTypes.KU_QU, save.getId(), save.getCode(), save.getName(), null, save.getMajorHazardId(),
                save.getLocation());
        // 绑定监测点
        bpDeviceFacilityCollectionService.bindDeviceAndMonitorPointsByIds(device, pointId);
    }


    @Override
    public void edit(BpStoreAreaAddBean b) {
        relDao.deleteByRefTypeAndRefId("store_area", b.getId());
        BpStoreArea a = dao.findById(b.getId()).get();
        // a.setCode(b.getCode());
        a.setEnvFunctionArea(b.getEnvFunctionArea());
        a.setLocation(b.getLocation());


        a.setMajorHazardId(b.getMajorHazardId());
        a.setMajorHazardUnitCode(b.getMajorHazardUnitCode());
        a.setName(b.getName());
        a.setNearbyStoreHouseMinDistance(b.getNearbyStoreHouseMinDistance());
        a.setStoreaAreaSize(b.getStoreaAreaSize());
        a.setSurrEquipmentDistance(b.getSurrEquipmentDistance());
        a.setSurrSafetyDistance(b.getSurrSafetyDistance());
        BpStoreArea save = dao.save(a);
        List<Long> pointId = b.getPointId();
        for (Long aLong : pointId) {
            BpMonitoringPointRel bpMonitoringPointRel = new BpMonitoringPointRel();
            bpMonitoringPointRel.setRefType("store_area");
            bpMonitoringPointRel.setRefId(save.getId());
            bpMonitoringPointRel.setMonitoringPointId(aLong);
            relDao.save(bpMonitoringPointRel);
        }
        addOrUpdateDeviceFacilityCollection(save, pointId);
    }

    @Override
    public BpStoreAreaAddBean getById(Long id) {
        BpStoreArea entity = dao.findById(id).get();
        List<BpMonitoringPointRel> all1 = relDao.findByRefTypeAndRefId("store_area", id);
        BpStoreAreaAddBean bpStoreAreaAddBean = new BpStoreAreaAddBean();

        bpStoreAreaAddBean.setId(entity.getId());
        bpStoreAreaAddBean.setCode(entity.getCode());
        bpStoreAreaAddBean.setCreateBy(entity.getCreateBy());
        bpStoreAreaAddBean.setCreateTime(entity.getCreateTime());
        bpStoreAreaAddBean.setDeleteFlag(entity.getDeleteFlag());
        bpStoreAreaAddBean.setEnvFunctionArea(entity.getEnvFunctionArea());
        bpStoreAreaAddBean.setLocation(entity.getLocation());
        bpStoreAreaAddBean.setUpdateTime(entity.getUpdateTime());
        bpStoreAreaAddBean.setUpdateBy(entity.getUpdateBy());
        bpStoreAreaAddBean.setTenentId(entity.getTenentId());
        bpStoreAreaAddBean.setSurrSafetyDistance(entity.getSurrSafetyDistance());
        bpStoreAreaAddBean.setSurrEquipmentDistance(entity.getSurrEquipmentDistance());
        bpStoreAreaAddBean.setStoreaAreaSize(entity.getStoreaAreaSize());
        bpStoreAreaAddBean.setOrgCode(entity.getOrgCode());
        bpStoreAreaAddBean.setNearbyStoreHouseMinDistance(entity.getNearbyStoreHouseMinDistance());
        bpStoreAreaAddBean.setName(entity.getName());
        if(entity.getMajorHazardId() !=null){
            MhMajorHazard one = mhMajorHazardDao.getOne(entity.getMajorHazardId());
            bpStoreAreaAddBean.setMajorHazardUnit(one.getName());
        }
        bpStoreAreaAddBean.setMajorHazardId(entity.getMajorHazardId());
        bpStoreAreaAddBean.setMajorHazardUnitCode(entity.getMajorHazardUnitCode());
        ArrayList<MhMonitoringPointBean> objects = new ArrayList<>();
        if(all1 !=null){
            for (BpMonitoringPointRel bpMonitoringPointRel : all1) {
                MhMonitoringPointBean byId = mhMonitoringPointService.getById(bpMonitoringPointRel.getMonitoringPointId());
                objects.add(byId);
            }
        }
        bpStoreAreaAddBean.setMhMonitoringPointBeans(objects);
        QBpStoreHouse bpStoreHouse = QBpStoreHouse.bpStoreHouse;
        Predicate predicate = bpStoreHouse.isNotNull();
        predicate = entity.getCode() == null ? predicate : ExpressionUtils.and(predicate, bpStoreHouse.storeAreaCode.eq(entity.getCode()));
        Iterable<BpStoreHouse> all = bpStoreHouseDao.findAll(predicate);
        int a=0;
        for (BpStoreHouse storeHouse : all) {
                a++;
        }
        bpStoreAreaAddBean.setHouses(a);
        return bpStoreAreaAddBean;
    }
}
